package com.javasea.web.schedule.springtask.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

@RestController
@RequestMapping("tasks")
public class DoTaskService {

	@Autowired
    private AsyncTask asyncTask;

    @RequestMapping("test1")
    public String test1() throws Exception {

    	long start = System.currentTimeMillis();

    	Future<Boolean> a = asyncTask.doTask11();
    	Future<Boolean> b = asyncTask.doTask22();
    	Future<Boolean> c = asyncTask.doTask33();

    	while (!a.isDone() || !b.isDone() || !c.isDone()) {
    		if (a.isDone() && b.isDone() && c.isDone()) {
    			break;
    		}
    	}

    	long end = System.currentTimeMillis();

    	String times = "任务全部完成，总耗时：" + (end - start) + "毫秒";
    	System.out.println(times);

    	return times;
    }

    @RequestMapping("test2")
    public String test2() throws Exception {

        long start = System.currentTimeMillis();

        List<Future<Boolean>> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Future<Boolean> a = asyncTask.doTaskAA(i + "");
            list.add(a);
        }

        for (Future f : list) {
            Object o = f.get();
        }
        long end = System.currentTimeMillis();


        String times = "任务全部完成，总耗时：" + (end - start) + "毫秒";
        System.out.println(times);

        return times;
    }
}
